package com.ruoyi.wechat.tools;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

public class tool {

    // 文本内容安全检测
    public static boolean checkText(String accessToken, String content) {
        String url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken;
        JSONObject param = new JSONObject();
        param.put("content", content);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(param.toJSONString(), headers);

        ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
        JSONObject result = JSONObject.parseObject(response.getBody());
        // errcode为0表示安全
        return result.getIntValue("errcode") == 0;
    }

    // 多媒体内容安全检测（异步）
    public static boolean checkMedia(String accessToken, String mediaUrl, int mediaType) {
        String url = "https://api.weixin.qq.com/wxa/media_check_async?access_token=" + accessToken;
        JSONObject param = new JSONObject();
        param.put("media_url", mediaUrl);
        param.put("media_type", mediaType); // 1:音频 2:图片

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(param.toJSONString(), headers);

        ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
        JSONObject result = JSONObject.parseObject(response.getBody());
        // errcode为0表示提交成功，具体检测结果需回调
        return result.getIntValue("errcode") == 0;
    }
}